package servidor;
import interfaces.IServidorConsultaIndices;

import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import com.google.gson.Gson;


public class ServidorConsultaIndicesREST extends UnicastRemoteObject implements IServidorConsultaIndices {

	private Gson gson = new Gson();
	private RESTGet RESTGet = new RESTGet();
	private static final long serialVersionUID = 1L;
	
	public ServidorConsultaIndicesREST() throws RemoteException {
		super();
	}

	public float converterDolarReal(float valor) throws RemoteException {
		Float returnFloat = 0.0f;
		String requestString = "http://www.google.com/ig/calculator?hl=en&q=" + valor + "USD%3D%3FBRL";
		try {
			String response = RESTGet.httpGet(requestString);
			System.out.println(response);
			Conversion conv = gson.fromJson(response, Conversion.class);
			returnFloat = Float.parseFloat(conv.getRhs().split(" ")[0]);			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return returnFloat.floatValue();
	}

	public float converterEuroReal(float valor) throws RemoteException {
		Float returnFloat = 0.0f;
		String requestString = "http://www.google.com/ig/calculator?hl=en&q=" + valor + "EUR%3D%3FBRL";
		try {
			String response = RESTGet.httpGet(requestString);
			System.out.println(response);
			Conversion conv = gson.fromJson(response, Conversion.class);
			returnFloat = Float.parseFloat(conv.getRhs().split(" ")[0]);			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return returnFloat.floatValue();
	}

	public float consultarIOF() throws RemoteException {
		return 0.38f;
	}
	
	public static void main(String[] args) {
		ServidorConsultaIndicesREST servidorConsultaIndicesREST;
		try {
			servidorConsultaIndicesREST = new ServidorConsultaIndicesREST();
			System.out.println(servidorConsultaIndicesREST.converterDolarReal(1));
			System.out.println(servidorConsultaIndicesREST.converterEuroReal(1));
			System.out.println(servidorConsultaIndicesREST.consultarIOF());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
}
